<?php
session_start();
// 权限验证
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] != 1) {
    header('Location: login.php');
    exit;
}

// 数据库配置
$host = 'localhost';
$dbname = 'itops_help1_center';
$username = 'itops_help1_center';
$password = 'itops_help1_center';

// 获取用户总数用于统计（完善错误处理）
$userCount = 0;
try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );

    $stmt = $pdo->query("SELECT COUNT(*) as count FROM user");
    
    if ($stmt instanceof PDOStatement) {
        $result = $stmt->fetch();
        $userCount = $result['count'] ?? 0;
    } else {
        $userCount = 0;
    }
} catch (PDOException $e) {
    $userCount = 0;
}

// 获取文章总数
$articleCount = 0;
try {
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM article");
    if ($stmt instanceof PDOStatement) {
        $result = $stmt->fetch();
        $articleCount = $result['count'] ?? 0;
    }
} catch (PDOException $e) {
    $articleCount = 0;
}

// 获取分类总数
$categoryCount = 0;
try {
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM category");
    if ($stmt instanceof PDOStatement) {
        $result = $stmt->fetch();
        $categoryCount = $result['count'] ?? 0;
    }
} catch (PDOException $e) {
    $categoryCount = 0;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理首页 - IT知识库管理系统</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background-color: #F1F5F9;
            color: #1E293B;
            line-height: 1.5;
        }
        
        /* 布局容器 */
        .admin-container {
            display: flex;
            min-height: 100vh;
        }
        
        /* 侧边栏样式 */
        .admin-sidebar {
            width: 240px;
            background-color: #1E293B;
            color: white;
            padding: 1.5rem 0;
            flex-shrink: 0; /* 防止侧边栏被压缩 */
        }
        
        .sidebar-logo {
            display: flex;
            align-items: center;
            padding: 0 1.5rem 1.5rem;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            margin-bottom: 1rem;
        }
        
        .sidebar-logo i {
            font-size: 1.5rem;
            margin-right: 0.75rem;
            color: #38BDF8;
        }
        
        .nav-list {
            list-style: none;
        }
        
        .nav-item {
            margin-bottom: 0.25rem;
        }
        
        .nav-link {
            display: flex;
            align-items: center;
            padding: 0.75rem 1.5rem;
            color: #CBD5E1;
            text-decoration: none;
            transition: all 0.2s;
        }
        
        .nav-link:hover, .nav-link.active {
            background-color: #334155;
            color: white;
        }
        
        .nav-link i {
            width: 1.25rem;
            margin-right: 0.75rem;
        }
        
        /* 内容区样式 */
        .admin-content {
            flex: 1;
            padding: 1.5rem;
            overflow-y: auto;
        }
        
        .content-wrapper {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #E2E8F0;
        }
        
        .page-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #1E293B;
        }
        
        /* 统计卡片样式 */
        .stats-container {
            display: flex;
            flex-wrap: wrap; /* 自动换行 */
            gap: 1rem;
            margin-bottom: 1.5rem;
        }
        
        .stat-card {
            flex: 1;
            min-width: 200px; /* 最小宽度，确保小屏幕显示正常 */
            background: white;
            padding: 1.25rem;
            border-radius: 0.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .stat-label {
            color: #64748B;
            font-size: 0.875rem;
            margin-bottom: 0.5rem;
        }
        
        .stat-value {
            font-size: 1.875rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: #1E293B;
        }
        
        .stat-trend {
            font-size: 0.75rem;
            color: #10B981;
            display: flex;
            align-items: center;
        }
        
        .stat-trend i {
            margin-right: 0.25rem;
            font-size: 0.875rem;
        }
        
        /* 表格样式 */
        .table-container {
            background: white;
            border-radius: 0.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .data-table th,
        .data-table td {
            padding: 0.75rem 1rem;
            text-align: left;
            border-bottom: 1px solid #E2E8F0;
        }
        
        .data-table th {
            background-color: #F8FAFC;
            font-weight: 600;
            color: #64748B;
            font-size: 0.875rem;
        }
        
        .data-table tr:last-child td {
            border-bottom: none;
        }
        
        /* 按钮样式 */
        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            font-weight: 500;
            text-decoration: none;
            cursor: pointer;
            transition: all 0.2s;
            border: none;
            font-size: 0.875rem;
        }
        
        .btn-primary {
            background-color: #3B82F6;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #2563EB;
        }
        
        .btn-outline {
            background-color: transparent;
            border: 1px solid #94A3B8;
            color: #64748B;
        }
        
        .btn-outline:hover {
            background-color: #F1F5F9;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .admin-container {
                flex-direction: column;
            }
            
            .admin-sidebar {
                width: 100%;
                padding: 1rem 0;
            }
            
            .admin-content {
                padding: 1rem;
            }
            
            .page-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 1rem;
            }
            
            .table-container {
                overflow-x: auto; /* 表格横向滚动 */
            }
            
            .data-table {
                min-width: 600px; /* 确保表格内容不挤压 */
            }
        }
    </style>
</head>
<body>
    <div class="admin-container">
        <!-- 侧边栏 -->
        <aside class="admin-sidebar">
            <div class="sidebar-header">
                <div class="sidebar-logo">
                    <i class="fas fa-cogs"></i>
                    <span>知识库管理</span>
                </div>
            </div>
            <ul class="nav-list">
                <li class="nav-item">
                    <a href="index.php" class="nav-link active">
                        <i class="fas fa-home"></i>
                        <span>管理首页</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="article.php" class="nav-link">
                        <i class="fas fa-file-alt"></i>
                        <span>文章管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="category.php" class="nav-link">
                        <i class="fas fa-folder"></i>
                        <span>分类管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="version.php" class="nav-link">
                        <i class="fas fa-sync-alt"></i>
                        <span>版本管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="user.php" class="nav-link">
                        <i class="fas fa-users"></i>
                        <span>用户管理</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="logout.php" class="nav-link">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>退出登录</span>
                    </a>
                </li>
            </ul>
        </aside>

        <!-- 内容区 -->
        <main class="admin-content">
            <div class="content-wrapper">
                <div class="page-header">
                    <h1 class="page-title">管理首页</h1>
                    <div>
                        <span>欢迎回来，<?php echo htmlspecialchars($_SESSION['username'] ?? '管理员'); ?></span>
                    </div>
                </div>

                <!-- 管理概览卡片 -->
                <div class="stats-container">
                    <div class="stat-card">
                        <div class="stat-label">文章总数</div>
                        <div class="stat-value"><?php echo $articleCount; ?></div>
                        <div class="stat-trend">
                            <i class="fas fa-arrow-up"></i> 较上周增长 5%
                        </div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-label">分类总数</div>
                        <div class="stat-value"><?php echo $categoryCount; ?></div>
                        <div class="stat-trend">
                            <i class="fas fa-arrow-up"></i> 较上周增长 1
                        </div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-label">用户总数</div>
                        <div class="stat-value"><?php echo $userCount; ?></div>
                        <div class="stat-trend">
                            <i class="fas fa-arrow-up"></i> 较上周增长 3
                        </div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-label">版本记录</div>
                        <div class="stat-value">8</div>
                        <div class="stat-trend">
                            <i class="fas fa-arrow-up"></i> 较上周增长 1
                        </div>
                    </div>
                </div>

                <!-- 最近更新 -->
                <div class="table-container">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>最近更新</th>
                                <th>类型</th>
                                <th>时间</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>新增文章《PHP数组操作指南》</td>
                                <td>文章</td>
                                <td>2025-10-18 14:30</td>
                            </tr>
                            <tr>
                                <td>新增用户「editor01」</td>
                                <td>用户</td>
                                <td>2025-10-18 11:20</td>
                            </tr>
                            <tr>
                                <td>更新版本 1.2.0</td>
                                <td>版本</td>
                                <td>2025-10-17 09:15</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </main>
    </div>
</body>
</html>